package com.hbyj.struts.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.CDBInfo;
import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.Functions;
import com.hbyj.hbm.vo.Jbqk01;
import com.hbyj.hbm.vo.Jbqk01temp;
import com.hbyj.hbm.vo.YjczYjzy01;
import com.hbyj.hbm.vo.YjczYjzy02;
import com.hbyj.hbm.vo.YjczYpxx01;
import com.hbyj.util.ChangeTool;
import com.hbyj.util.Constants;
import com.hbyj.util.JsonUtil;
import com.hbyj.util.PageClass;
import com.opensymphony.xwork2.ActionSupport;

public class BaseInfoQueryAction extends ActionSupport{
	private int total;
	private List rows = new ArrayList();
	private List groupList = new ArrayList();
	private String jsonResult;
	private Jbqk01 jbqk = new Jbqk01();
	public String getJsonResult() {
		return jsonResult;
	}
	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public List getRows() {
		return rows;
	}
	public void setRows(List rows) {
		this.rows = rows;
	}
	
	public List getGroupList() {
		return groupList;
	}
	public void setGroupList(List groupList) {
		this.groupList = groupList;
	}
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 基本查询
	 * @return
	 */
	public String findJbqk01ListByCondition(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String areaCode = request.getParameter("region");
		String zhdType = request.getParameter("zhdType");
		String name = request.getParameter("name");
		areaCode = ChangeTool.ChangeArea(areaCode);
		String tableName = "";
		String propertyName = "";
		String hql = "";
		String countHql = "";
		String conStr = "";
		String conditionHql = "";
		String groupCountHql = "";//分组统计Hql
		if(!zhdType.equals("all"))
			conStr = " and jbqk01.id.jbqk0102='"+zhdType+"'";
		/*else{
			switch(Integer.parseInt(zhdType)){
			case 1:
				tableName = "";
				break;
			case 2:break;
			case 3:break;
			case 4:break;
			case 5:break;
			}
		}*/
		//hql = "select distinct jbqk01.id.jbqk0100,jbqk01.jbqk0110,jbqk01.jbqk0120, jbqk01.jbqk0130,jbqk01.jbqk0140,jbqk01.jbqk0150,jbqk01.jbqk0160,jbqk01.jbqk0170 from Jbqk01 as jbqk01,"+tableName+" as subTbl where jbqk01.id.jbqk0100=subTbl."+propertyName+" and jbqk01.id.jbqk0100 like '"+areaCode+"%'"+conStr;
		//countHql = "select count(distinct jbqk01.id.jbqk0100)   from Jbqk01 as jbqk01,"+tableName+" as subTbl  where jbqk01.id.jbqk0100=subTbl."+propertyName+" and jbqk01.id.jbqk0100 like '"+areaCode+"%'"+conStr;
		conditionHql = "jbqk01.id.jbqk0100 like '"+areaCode+"%' and jbqk01.jbqk0110 like '%"+name+"%'"+conStr;
		hql = "select distinct jbqk01.id.jbqk0100,jbqk01.jbqk0110,jbqk01.jbqk0120, jbqk01.jbqk0130,jbqk01.jbqk0140,jbqk01.jbqk0150,jbqk01.jbqk0160,jbqk01.jbqk0170,jbqk01.jbqk0180,jbqk01.jbqk0190,jbqk01.id.jbqk0102 from Jbqk01 as jbqk01 where   "+conditionHql;
		countHql = "select count(distinct jbqk01.id.jbqk0100)   from Jbqk01 as jbqk01  where  "+conditionHql;
		groupCountHql = "select jbqk01.id.jbqk0102,count(jbqk0102) from Jbqk01 as jbqk01  where   "+conditionHql+" group by jbqk01.id.jbqk0102";
		 groupList = DbTools.findByHql(groupCountHql);
		int t = DbTools.getDataHQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		//List<List> list = (List)DbTools.findPageHQL(first, max,hql);
		rows = DbTools.parseBaleInfoObj(DbTools.findPageHQL(first, max,hql));
		total = t;
		return SUCCESS;
	}
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 查询放在预案基本信息
	 * @return
	 */
	public String findFzyaDetailsByCode(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String code = request.getParameter("code");
		String zhdName = request.getParameter("zhdName");
		String hql = " from Jbqk01 ,FzyaFzya03  where jbqk0100=fzya03000 and fzya03000='"+code+"'";
		rows = DbTools.findPageHQL(0,1,hql);
		request.setAttribute("fzyaDetails", rows);
		request.setAttribute("zhdName", zhdName);
		return SUCCESS;
	}
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 高级搜索
	 * @return
	 */
	public String findJbqk01ListByAdvanceCondition(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		String region = request.getParameter("region");
		String zhdType = request.getParameter("zhdType");
		String zhdGroupType = request.getParameter("zhdGroupType");
		String graphParam = request.getParameter("graphParam");//图形范围参数  圆  x,y,r  矩形 xMin,yMin,xMax,yMax
		int pageSize = Constants.PAGE_KEY;
		if(pageNo==null){
			pageSize = 1000;
			pageNo = "1";
		}
		String countHql = "";
		String conStr = "";
		String conditionHql = "";
		String groupCountHql = "";//分组统计Hql
		String hql = "";
		region = ChangeTool.ChangeArea(region);
		if(!"".equals(zhdType)){
			conStr +=" and substring(jbqk0100,7,2) in ("+zhdType+")";
		}
		if(!"".equals(graphParam)){
			String[] graphParamArr = graphParam.split(",");
			if(graphParamArr.length==3){
				conStr +=" and ((jbqk0120-"+graphParamArr[0]+")*(jbqk0120-"+graphParamArr[0]+")+(jbqk0130-"+graphParamArr[1]+")*(jbqk0130-"+graphParamArr[1]+"))< "+graphParamArr[2]+"*"+graphParamArr[2];
			}else{
				conStr +=" and jbqk0120 >"+graphParamArr[0]+" and jbqk0120<"+graphParamArr[2]+" and  jbqk0130>"+graphParamArr[1]+" and  jbqk0130<"+graphParamArr[3];
			}
			
		}
		if(!"".equals(zhdGroupType)){
			conStr += " and jbqk01.id.jbqk0102 in ("+zhdGroupType+")";
		}
		conditionHql = "jbqk01.id.jbqk0100 like '"+region+"%' " + conStr;
		hql = "select distinct jbqk01.id.jbqk0100,jbqk01.jbqk0110,jbqk01.jbqk0120, jbqk01.jbqk0130,jbqk01.jbqk0140,jbqk01.jbqk0150,jbqk01.jbqk0160,jbqk01.jbqk0170,jbqk01.jbqk0180,jbqk01.jbqk0190,jbqk01.id.jbqk0102 from Jbqk01 as jbqk01 where   "+conditionHql;
		countHql = "select count(distinct jbqk01.id.jbqk0100)   from Jbqk01 as jbqk01  where  "+conditionHql;
		groupCountHql = "select jbqk01.id.jbqk0102,count(jbqk0102) from Jbqk01 as jbqk01  where   "+conditionHql+" group by jbqk01.id.jbqk0102";
		 groupList = DbTools.findByHql(groupCountHql);
		int t = DbTools.getDataHQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		//List<List> list = (List)DbTools.findPageHQL(first, max,hql);
		rows = DbTools.parseBaleInfoObj(DbTools.findPageHQL(first, max,hql));
		total = t;
		return SUCCESS;
	}
	
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 点击图片后，查看基本详情信息
	 * @return
	 */
	public String lookIntroduce(){
		HttpServletRequest request = ServletActionContext.getRequest();
		//地质灾害统一编码
		String name = request.getParameter("name");//灾点名称
		String conURL = request.getParameter("conURL");//详情名称  word
		String file = request.getParameter("fileName");
		String webRootPath = ServletActionContext.getServletContext().getRealPath("/")+"uploadfolder\\simple\\"+file+"\\document\\";
		if(!conURL.equals("null")){
			conURL = webRootPath + conURL ;
			String[] con= ChangeTool.getWordContent(conURL);
			request.setAttribute("Contetnt", con);
		}
		String imgURL = request.getParameter("imgURL");//图片路径
		request.setAttribute("file", file);
		request.setAttribute("name", name);
		request.setAttribute("imgURL", imgURL);
		return SUCCESS;
	}
	public String findJCDCBaseInfoByCode(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String code = request.getParameter("code");
		String flag = code.substring(6, 8);
		String zhdName = request.getParameter("zhdName");
		String hql = " from Jbqk01 ,JcdcJcdc"+flag+"  where jbqk0100=jcdc"+flag+"001 and jcdc"+flag+"001='"+code+"'";
		rows = DbTools.findPageHQL(0,1,hql);
		request.setAttribute("fzyaDetails", rows);
		request.setAttribute("zhdName", zhdName);
		
		return SUCCESS;
	}
	
}
